<template>
  <div class="goods-image">
    <div class="large"
    v-show="!isOutside"
    :style="[
    {backgroundImage:`url(${imageList[curIndex]})`},
    {backgroundPosition:`${bg.x}px ${bg.y}px`}
    ]"
    >
    </div>
    <div class="middle" ref="target">
      <img :src="imageList[curIndex]" alt="">
      <!-- 蒙层 -->
      <div class="layer" :style="{left:left+'px',top:top+'px'}" v-show="!isOutside"></div>
    </div>
    <ul class="small">
      <li v-for="(img,i) in imageList" :key="i" @mouseenter="curIndex=i" :class="{active:curIndex===i}">
        <img :src="img" alt="">
      </li>
    </ul>
  </div>
</template>
<script>
import { ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    imageList: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    const curIndex = ref(0)
    const target = ref(null)
    // 获取鼠标相对于目标移动盒子的位置
    const { elementX, elementY, isOutside } = useMouseInElement(target)

    const left = ref(0)
    const top = ref(0)
    // 图片放大
    const bg = ref({ x: 0, y: 0 })
    watch([elementX, elementY], () => {
      // console.log(elementX, elementY)
      // x轴
      if (elementX.value < 100) {
        left.value = 0
      } else if (elementX.value > 300) {
        left.value = 200
      } else {
        left.value = elementX.value - 100
      }
      // y轴
      if (elementY.value < 100) {
        top.value = 0
      } else if (elementY.value > 300) {
        top.value = 200
      } else {
        top.value = elementY.value - 100
      }
      // 放大图片
      bg.value.x = -left.value * 2
      bg.value.y = -top.value * 2
    })
    return { curIndex, target, isOutside, left, top, bg }
  }
}
</script>
<style scoped lang="less">
 .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    z-index:500;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    .layer {
  width: 200px;
  height: 200px;
  background: rgba(0, 0, 0, 0.2);
  left: 0;
  top: 0;
  position: absolute;
}
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,&.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
